package com.liu.communitysystemend.module.visitor.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.communitysystemend.exception.CommonException;
import com.liu.communitysystemend.exception.SavaException;
import com.liu.communitysystemend.module.visitor.domain.Visitor;
import com.liu.communitysystemend.module.visitor.domain.Visitor;
import com.liu.communitysystemend.module.visitor.mapper.VisitorMapper;
import com.liu.communitysystemend.module.visitor.service.VisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
*
*/
@Service
public class VisitorServiceImpl implements VisitorService {
    
    @Autowired
    private VisitorMapper visitorMapper;

    @Override
    public Visitor getVisitorById(String id) {
        if (id == null) {
            throw new CommonException(501, "参数为空");
        }
        LambdaQueryWrapper<Visitor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Visitor::getId,id);
        Visitor visitor = visitorMapper.selectOne(wrapper);
        return visitor;
    }
    public Visitor selectOneById(String id){
        Visitor visitor=visitorMapper.selectById(id);
        return  visitor;
    }

    @Override
    public Visitor update(Visitor visitor) {
        LambdaQueryWrapper<Visitor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Visitor::getId,visitor.getId());
        int update = visitorMapper.update(visitor, wrapper);
        if (update>0){
            return visitor;
        }
        throw new CommonException(502,"更新失败");
    }

    @Override
    public Visitor add(Visitor visitor) {
        if(visitor ==null){
            throw new SavaException("插入用户失败,菜单的类型实体为空");
        }
        int insert=visitorMapper.insert(visitor);
        if(insert>0){
            return visitor;
        }
        throw new SavaException("插入用户失败");
    }

    @Override
    public Page<Visitor> getPage(Visitor visitor) {
        LambdaQueryWrapper<Visitor> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Visitor::getIsDeleted,0);
        Page<Visitor> page=new Page<>(visitor.getPage().getPage(),visitor.getPage().getSize());
        Page<Visitor> poiPage=(Page<Visitor>) visitorMapper.selectPage(page,wrapper);
        return poiPage;
    }

    @Override
    public Integer deleteById(String id) {
        Visitor visitor = selectOneById(id);
        if(visitor==null){
            throw new CommonException(4004,"找不到id为:"+id+"的数据");
        }
        int i=visitorMapper.deleteById(id);
        return i;
    }

    @Override
    public Integer test() {
        for (int i = 0; i < 20; i++) {
            Visitor visitor = new Visitor();
            visitor.setId("100"+i);
            visitor.setCarNo("44243222323232"+i);
            visitor.setDays(3);
            visitor.setRoomId("100"+i);
            visitor.setUserId("100"+i);
            visitor.setReason("来访做客");
            visitor.setVisitorNum(10);
            visitor.setCarNo("粤A1U4"+i);
            visitor.setName("来访者"+i);
            visitor.setVisitorPhone("163622818112"+i);
            visitor.setVisitorTime(new Date());
            visitorMapper.insert(visitor);
        }
        return 1;
    }
}
